<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { getAdminLogin } from '@/api/admin.js'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const form = ref()
const formModel = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入管理员用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入管理员密码', trigger: 'blur' }
  ]
}

const gologin = async () => {
  await form.value.validate()

  // 硬编码验证 + 调用后端接口
  if (formModel.value.username === 'admin' && formModel.value.password === '123456') {
    try {
      // 调用后端管理员登录接口
      const response = await getAdminLogin({
        username: formModel.value.username,
        password: formModel.value.password
      })
      console.log(response)

      // 使用独立的管理员token存储
      localStorage.setItem('adminToken', 'admin-token-' + Date.now())
      ElMessage.success('管理员登录成功')
      router.push('/admin')
    } catch (error) {
      console.log(error)
      ElMessage.error('管理员登录失败')
    }
  } else {
    ElMessage.error('管理员账号或密码错误')
  }
}

const goBack = () => {
  // 清除管理员token并返回首页
  localStorage.removeItem('adminToken')
  router.push('/')
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="24" class="form">
      <el-form ref="form" :model="formModel" :rules="rules" size="large" autocomplete="off">
        <el-form-item>
          <h1>管理员登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input :prefix-icon="User" placeholder="请输入管理员用户名" v-model="formModel.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入管理员密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="gologin">
            管理员登录
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button @click="goBack" style="width: 100%" plain>
            返回首页
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<!-- 修改后的样式 -->
<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-image:url(@/assets/background.jpg);
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    background: white;
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    width: 100%;
    max-width: 400px;

    .button {
      width: 100%;
      background: #667eea;
      border-color: #667eea;
    }

    .tip {
      text-align: center;
      margin-top: 10px;
    }
  }
}
</style>
